************************************************
*Figure A1: Robots along the skill distribution*
************************************************

*Adapted version of Autor and Dorn skill distribution (2013)
use "$contributed_data/occ-means-by-decade-1980-2005-czall" if year==1980, clear

rename occ1990dd occ1990

*Graetz and Michaels xwalk
merge m:1 occ1990 using "$contributed_data/replaceability_occ", keep(3) nogenerate

rename occ1990 occ1990dd

* Cannot include occs with missing replaceable
assert replaceable!=.

* Assert that no farm occs are present
assert occ1990dd<473 | occ1990dd>498

* Assign weights
sort occ_mn_wg
summ occ_wt
egen yrwt=sum(occ_wt)
gen sharewt=occ_wt/yrwt
summ occ_wt sharewt

sort occ_mn_wg
gen runwt=sum(sharewt)
gen cumwt=runwt
replace cumwt=cumwt*100
summ cumwt

local occs = _N
gen left=cumwt[_n-1]
gen right=cumwt
replace left=0 in 1
gen margwt=right-left

quietly {
forvalues perc=1(1)100 {

  gen p`perc'=0
  forvalues occrank=1(1)`occs' {

    ** Four cases
    * 1. Right overlap
    if right[`occrank']>`perc'-1 & right[`occrank']<=`perc' & left[`occrank']<`perc'-1 {
      replace p`perc'= right-(`perc'-1) in `occrank'
    }

    * 2. Left overlap
    if left[`occrank']>=`perc'-1 & left[`occrank']<`perc' & right[`occrank']> `perc'{
      replace p`perc'= `perc'-left in `occrank'
    }

    * 3. Full overlap
    if left[`occrank'] <`perc'-1 & right[`occrank']>= `perc' {
      replace p`perc'= 1 in `occrank'
    }

    * 4. Full underlap (including exact match)
    if left[`occrank'] >= `perc'-1 & right[`occrank'] <= `perc' {
      replace p`perc'= right-left in `occrank'
    }
  }
}
}

/*
  The matrix we now need is psh by occ1990dd
  We multiply these shares by occupation shares of employment to get
  the total share in each percentile bin
  This share is 1 percent in 1980 by construction
  * Obtain total of shares by percentile
*/

forvalues perc=1(1)100 {
  gen psh`perc'=p`perc'/margwt
  egen empsh`perc'=sum(psh`perc'*margwt)
}

summ psh* empsh*
keep occ1990dd psh1-psh100 R33_3a hsd hsg smc clg year replaceable
sort occ1990dd

* Calculate weighted mean of R33_3a
forvalues perc=1(1)100 {
	capture drop percwt
	egen percwt=sum(psh`perc')
	* RTI measures that apply to all workers
	foreach v in R33_3a replaceable {
		egen perc_`v'_`perc' = sum(psh`perc'*`v'/percwt)
	}
}
keep if _n==1
drop psh* empsh* R* hsd hsg smc clg
desc , f
reshape long perc_R33_3a_ perc_replaceable_, i(year) j(pctile)
rename perc_R33_3a_ perc_R33a

label var perc_R33a "Routine Occupation Share 1980"
sort pctile
label data "RSH by Percentile in 1980"
desc
summ


lowess perc_replaceable_ pctile, gen(robots) bwidth(.3) nograph

twoway (line robots pctile,  color(black) lpattern(solid) msize(vsmall)) ///
   , xtitle("Skill percentile" "(ranked by 1980 occupational mean wage)") ytitle("")  ylabel(0(0.05).5, angle(horiz) grid gmin gmax format(%5.2f)) ///
	l1title("Share of occupations" "that are replaceable by robots") ///
	graphregion(color(white) fcolor(white))
	graph export "$figures_appendix/Figure A1.png", replace
